package ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;


import entity.User;
import net.sf.ezmorph.bean.MorphDynaBean;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import thread.QunFa;

public class QQFrame extends JFrame{
	private JButton sendBtn;
	private static JTextArea ta;
	private JTextField tf;
	private JPanel mainPanel;
	private static JPanel sidePanel;
	private static JScrollPane jsp;
	private JScrollPane tajsp;
	private static Map<String,User> map;
	private static List<Map<String,User>> list;
	private static Socket socket;
	private static String username;
	private static String password;
//	private String hisusername = null;
	public void init(){
		this.setTitle(username);
		this.setLayout(new BorderLayout());
		ta=new JTextArea(6,20);
		tf=new JTextField();
		mainPanel=new JPanel(new BorderLayout());
		sidePanel=new JPanel(new FlowLayout());
		
		jsp=new JScrollPane(sidePanel);
		tajsp=new JScrollPane(ta);
		sidePanel.setPreferredSize(new Dimension(100, 300));//锟斤拷锟矫达拷小锟斤拷锟斤拷锟轿匡拷
		for (Map<String, User> map : list) {
			Set<String> set = map.keySet();
			String hisusername=null;
			for (String string : set) {
				hisusername=string;
			}
			JButton btn=new JButton(hisusername);//灏唘sername鍔犲叆鎸夐挳
			btn.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					// TODO Auto-generated method stub
					try {
						OutputStream os=socket.getOutputStream();
						os.write("dakaichuangkou\n".getBytes());
						os.write(((btn.getText())+"\n").getBytes());//浼犻�掑鏂圭殑鍚嶅瓧
						os.write((username+"\n").getBytes());//浼犻�掓垜鐨勫悕瀛�
						System.out.println("hisusername"+(btn.getText())+"username"+username);
						P2PFrame p2p=new P2PFrame(btn.getText(),username,socket);
						QunFa.framemap1.put((btn.getText()), p2p);
					} catch (Exception e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				}
			});
			sidePanel.add(btn);
			
		}
		
		sendBtn=new JButton("send");
		
		mainPanel.add(tajsp,BorderLayout.NORTH);
		mainPanel.add(tf,BorderLayout.CENTER);
		mainPanel.add(sendBtn,BorderLayout.SOUTH);
		this.add(mainPanel,BorderLayout.CENTER);
		this.add(jsp, BorderLayout.EAST);
	}
	

		
	
	
	
	
	public void addEvent(){
		sendBtn.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
			    
			    try {
			    	String msg=	tf.getText();
			    	OutputStream os=socket.getOutputStream();
					os.write("qf\n".getBytes());
					os.write((username+":"+msg+"\n").getBytes());
					 tf.setText("");
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			   
			}
		});
	}
	public QQFrame() {
		// TODO Auto-generated constructor stub
		
		this.setSize(500, 300);
		this.setLocationRelativeTo(null);//锟津开匡拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷
		init();
		addEvent();
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//锟截闭达拷锟斤拷
		this.setVisible(true);//锟斤拷染
	}
	
	public static void main(String[] args) {
		
		try {
			String encoding=System.getProperty("sun.jnu.encoding");
		    socket=new Socket("127.0.0.1", 7001);
			InputStream is=socket.getInputStream();
			OutputStream os=socket.getOutputStream();
			boolean islogin=true;
	        GUIQQ qq ;
			os.write((encoding+"\n").getBytes());
			while(islogin)
			{
				qq=new GUIQQ();
				while(qq.getRuname()==null&&qq.getRpassword()==null){
					System.out.println(qq.getRuname());
				}
				boolean isture=true;
			
				username=qq.getRuname();
				password=qq.getRpassword();
			
			
			
				System.out.println(username);
				System.out.println(password);
				os.write((username+"\n").getBytes());
				os.write((password+"\n").getBytes());
				BufferedReader reader=new BufferedReader(new InputStreamReader(is));
				String q=reader.readLine();
				System.out.println(q);
				if((q).equals("loginsuccess")){
					qq.setVisible(false);
					qq.dispose();
					islogin=false;
				String json=reader.readLine();
//				System.out.println(json);
//				[{"锟斤拷锟斤拷":{"encoding":"GBK","socket":{"OOBInline":false,"bound":true,"channel":null,"closed":false,"connected":true,"inetAddress":{"MCGlobal":false,"MCLinkLocal":false,"MCNodeLocal":false,"MCOrgLocal":false,"MCSiteLocal":false,"address":[127,0,0,1],"anyLocalAddress":false,"canonicalHostName":"127.0.0.1","hostAddress":"127.0.0.1","hostName":"127.0.0.1","linkLocalAddress":false,"loopbackAddress":true,"multicastAddress":false,"siteLocalAddress":false},"inputShutdown":false,"inputStream":{"FD":{},"channel":null},"keepAlive":false,"localAddress":{"MCGlobal":false,"MCLinkLocal":false,"MCNodeLocal":false,"MCOrgLocal":false,"MCSiteLocal":false,"address":[127,0,0,1],"anyLocalAddress":false,"canonicalHostName":"127.0.0.1","hostAddress":"127.0.0.1","hostName":"127.0.0.1","linkLocalAddress":false,"loopbackAddress":true,"multicastAddress":false,"siteLocalAddress":false},"localPort":8888,"localSocketAddress":{"address":{"MCGlobal":false,"MCLinkLocal":false,"MCNodeLocal":false,"MCOrgLocal":false,"MCSiteLocal":false,"address":[127,0,0,1],"anyLocalAddress":false,"canonicalHostName":"127.0.0.1","hostAddress":"127.0.0.1","hostName":"127.0.0.1","linkLocalAddress":false,"loopbackAddress":true,"multicastAddress":false,"siteLocalAddress":false},"hostName":"127.0.0.1","hostString":"127.0.0.1","port":8888,"unresolved":false},"outputShutdown":false,"outputStream":{"FD":{},"channel":null},"port":26903,"receiveBufferSize":8192,"remoteSocketAddress":{"address":{"MCGlobal":false,"MCLinkLocal":false,"MCNodeLocal":false,"MCOrgLocal":false,"MCSiteLocal":false,"address":[127,0,0,1],"anyLocalAddress":false,"canonicalHostName":"127.0.0.1","hostAddress":"127.0.0.1","hostName":"127.0.0.1","linkLocalAddress":false,"loopbackAddress":true,"multicastAddress":false,"siteLocalAddress":false},"hostName":"127.0.0.1","hostString":"127.0.0.1","port":26903,"unresolved":false},"reuseAddress":false,"sendBufferSize":8192,"soLinger":-1,"soTimeout":0,"tcpNoDelay":false,"trafficClass":0},"username":"锟斤拷锟斤拷"}}]
				JSONArray ja = JSONArray.fromObject(json);
			
			
				list = JSONArray.toList(ja, HashMap.class);
//				System.out.println(list.size());
//				System.out.println(list.get(0).);
				}
				else{
					
					
					System.out.println("qq.getRuname():"+qq.getRuname());
					
					JOptionPane.showMessageDialog(null, "锟矫伙拷锟斤拷锟斤拷锟斤拷锟斤拷锟�", "", JOptionPane.ERROR_MESSAGE);
					qq.dispose();
				}
			}}	
		 catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		QQFrame frame=new QQFrame();
		new Thread(new QunFa(socket,ta,sidePanel,frame,jsp,username)).start();
		
		
	}
}
